package com.freshcity.page.common
{
	import com.freshcity.common.IsLimited;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.HBox;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.OpCodes;
	import com.freshcity.graphics.MessageStatus;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.utils.StringUtil;
	import com.maccherone.json.JSON;
	import com.pblabs.debug.Logger;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	public class UserInfoDetailPanel extends Component
	{
		[Embed(source='../assets/level-1.png')]
		public var Level1Img:Class;
		[Embed(source='../assets/level-2.png')]
		public var Level2Img:Class;
		[Embed(source='../assets/level-3.png')]
		public var Level3Img:Class;

		private var user:*;
		private var cond:*;
		private var mine:*;
		private var isLimitText:TextField;
		private var txtFormat:TextFormat;
		private var ypos:int=0;

		public function UserInfoDetailPanel(user:*, report:*)
		{
			txtFormat = new TextFormat("宋体", 12);
			txtFormat.leading = 6;
			
			isLimitText = new TextField();
			isLimitText.autoSize=TextFieldAutoSize.LEFT;
			//isLimitText.width=240;
			//isLimitText.multiline=true;
			//isLimitText.wordWrap=true;
			var myCSS:StyleSheet=new StyleSheet();
			myCSS.setStyle("a:link", {color: '#0000ff', textDecoration: 'none'});
			myCSS.setStyle("a:hover", {color: '#ff0000', textDecoration: 'underline'});
			isLimitText.styleSheet=myCSS;
			isLimitText.addEventListener(TextEvent.LINK, isLimitTextCallback);
			
			this.user=user;
			this.cond=(user.cond!='')?JSON.decode(user.cond):{};
			this.mine=GlobalVar.LOGIN_USER;
			this.mouseEnabled=false;
			super(null, 0, 0);
			this.addEventListener(Event.REMOVED_FROM_STAGE,dispose);
		}
		
		protected function isLimitTextCallback(event:TextEvent):void
		{
			if(mine.level>user.level){
				Logger.debug(this,"opcodes","REQ_LEVEL_UP:"+OpCodes.REQ_LEVEL_UP);
				main.commu.send(2310, {to: user.id}, onReqLevelUpCallBack);
			}else if(mine.level<user.level){
				main.statusPanel.showUpgrade(event);
				Dispatcher.addEventListener(EventName.LevelUp,redraw);
				//Dispatcher.dispatchEvent(new DataEvent(EventName.LevelUp));
			}
		}
		
		private function redraw(event:DataEvent):void
		{
			while(this.numChildren>0){
				this.removeChildAt(0);
			}
			this.ypos=0;
			this.mine=GlobalVar.LOGIN_USER;
			renderContent();
		}
		
		private function onReqLevelUpCallBack(data:*):void
		{
			main.msg.showRollTipsMsg("信息已发出");
			isLimitText.visible=false;
		}		

		override protected function addChildren():void
		{
			super.addChildren();
			renderContent();
			addBottomPadding();
			this.height = this.ypos+8;
		}

		override public function destroy():void
		{
			super.destroy();
		}
		
		private function dispose(event:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,dispose);
			if (isLimitText.hasEventListener(MouseEvent.CLICK))
			{
				isLimitText.removeEventListener(MouseEvent.CLICK, isLimitTextCallback);
			}
			if(Dispatcher.hasEventListener(EventName.LevelUp)){
				Dispatcher.removeEventListener(EventName.LevelUp,redraw);
			}
		}

		//添加留空
		private function addBottomPadding():void
		{
			var shape:Shape=new Shape();
			shape.graphics.beginFill(0, 0);
			shape.graphics.drawRect(0, 0, width, 5);
			shape.graphics.endFill();
			addChild(shape);
		}

		private function renderContent():void
		{
			createBaseInfo();
			//createFriendCond();
			//createUserDesc(this.user.selfDesc); 
			
			if (user.level > mine.level)
			{
				isLimitText.htmlText=""+"<a href='event:'>" + ">>" + "升级信用察看TA的完整信息</a>";
				this.ypos+=15;
				isLimitText.x = 220;
				isLimitText.y = this.ypos;
				this.ypos+=40;
				addChild(isLimitText);
			}else if(mine.level>user.level){
				isLimitText.htmlText=""+"<a href='event:'>" + ">>" + "对方信用级别太低，邀请TA升级信用</a>";
				this.ypos+=15;
				isLimitText.x = 200;
				isLimitText.y = this.ypos;
				this.ypos+=40;
				addChild(isLimitText);
			}
		}

		//基础资料
		private function createBaseInfo():void
		{
			var len:int=GlobalVar.nameProperty.length;
			var xpos:Number = 0.0;
			var j:int = 0;
			
			createTitleLabel("基本信息：");
			for (var i:int=0; i < len; i++)
			{
				var s1:String=GlobalVar.nameProperty[i];
				var s2:String=GlobalVar.labelProperty[i];
				if (mine[s1] && this.user[s1])
				{
					if(j%2==0){
						xpos = 15;
						ypos += 25;
					}else{
						xpos = 220;
					}
					createProperty(s2, this.user[s1],xpos,ypos);
					j++;
				}
			}
			ypos += 30;
		}
		
		//自我描述
		private function createUserDesc(data:String):void
		{
			createTitleLabel("交友独白：");
			ypos += 25;
			
			var contentTf:TextField=new TextField();
			contentTf.defaultTextFormat = txtFormat;
			contentTf.autoSize=TextFormatAlign.LEFT;
			contentTf.text = data;
			contentTf.width=320;
			contentTf.wordWrap=true;
			contentTf.x = 5;
			contentTf.y = ypos;
			addChild(contentTf);
			
			ypos += 80;
		}
		
		private function createFriendCond():void
		{
			if(this.user.qqNo == '' || this.cond.ageFrom == undefined) return;
			
			createTitleLabel("择友要求：");
			ypos += 25;
			createProperty('年龄', this.cond.ageFrom+" - "+this.cond.ageTo,15,ypos);
			
			var s1:String = "不限";
			if(this.cond.addCity!=''){
				s1 = this.cond.addProv + this.cond.addCity;
			}else if(this.cond.addProv!=''){
				s1 = this.cond.addProv;
			}
			
			createProperty('居住地', s1,220,ypos);
			ypos += 25;
			createProperty('身高', this.cond.tallFrom+" - "+this.cond.tallTo,15,ypos);
			
			var s2:String = '不限';
			if(this.cond.degree != ''){
				s2 = this.cond.degree+"及以上";
			}
			createProperty('学历', s2 ,220,ypos);
			ypos += 25;
		}
		
		private function createTitleLabel(label:String):void
		{
			var labelTf:TextField=new TextField();
			labelTf.defaultTextFormat = new TextFormat("黑体", 14);
			labelTf.autoSize=TextFormatAlign.LEFT;
			labelTf.text = label;
			labelTf.x = 5;
			labelTf.y = ypos;
			addChild(labelTf);
		}
		
		private function createProperty(label:String,value:String,xpos:Number=0,ypos:Number=0):void
		{
			var tf:TextField=new TextField();
			tf.defaultTextFormat = txtFormat;
			tf.autoSize=TextFormatAlign.LEFT;
			tf.height=20;
			tf.width=180;
			tf.htmlText = StringUtil.addColor(label + " ", "#666666") + value;
			tf.x = xpos;
			tf.y = ypos;
			addChild(tf);
		}		
	}
}
